/*上传头像*/
import config from './config';
import common from './common';
import store from 'store'


export default function(data, url, callback) {
	console.log('data', data);
    return new Promise((qres, qrej)=>{
        if (data) {
            let formData = {
                // "file":data,
				'user':'test'
            }
			let header ={
				// 'Content-Type':'application/json;charset=UTF-8',
				// chartset: "utf-8",
				// "content-type": "multipart/form-data"
			}
            if(store.state.userInfo && store.state.userInfo.token){
                formData.token = store.state.userInfo.token;
				header.token = store.state.userInfo.token;
            }
			console.log('header',header);
            uni.uploadFile({
                url,
                filePath: data,
                name: 'file',
				header,
                formData,
                success: (res) => {
                    console.log(res);
                    let resdata = JSON.parse(res.data);
                    console.log(resdata);
                    if (common.isDev()) {
                        console.log("up res data: " + JSON.stringify(resdata));
                    }
                    if (resdata.data && resdata.data.length) {
						console.log('成功');
                        qres(resdata.data)
                        common.callback(callback,resdata.data);
                    } else{
						console.log('失败');
                        common.showTip(resdata.msg,'none')
                        common.callback(callback);
                         
                    }
                },
                fail(err){
                    jres(err)
                }
            });
        } else {
            qres(null)
            common.callback(callback);
        }
    })
}